import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:tiktokadult/hj_model/acg/comic_chapters_model.dart';
import 'package:tiktokadult/hj_page/live/live_widget.dart';
import 'package:tiktokadult/hj_utils/widget_utils.dart';

import '../../assets_tool/app_colors.dart';

//有声小说 选择子集
class AudioPlayerBottomSheet extends StatefulWidget {
  final String title;
  final List<ComicChapterInfo> allAudiobooks;
  final Function(int)? indexHandler;
  final int? currentPlayIndex;

  AudioPlayerBottomSheet(
    this.title, {
    super.key,
    required this.allAudiobooks,
    this.indexHandler,
    this.currentPlayIndex,
  });

  @override
  State<AudioPlayerBottomSheet> createState() => _AudioPlayerBottomSheetState();
}

class _AudioPlayerBottomSheetState extends State<AudioPlayerBottomSheet> {
  int sortType = 1; //0-降序，1-生序

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 16, vertical: 13),
      constraints: BoxConstraints(maxHeight: Get.height / 2),
      decoration: BoxDecoration(
        color: Color(0xff030F18),
        borderRadius: BorderRadius.vertical(top: Radius.circular(18)),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            children: [
              Spacer(),
              Container(
                height: 5,
                width: 36,
                decoration: BoxDecoration(
                  color: Color(0xff989898),
                  borderRadius: BorderRadius.circular(100),
                ),
              ),
              Spacer(),
            ],
          ),
          18.sizeBoxH,
          Row(
            children: [
              Text(
                widget.title,
                style: TextStyle(
                  color: Colors.white,
                  fontSize: 16,
                  fontWeight: FontWeight.w500,
                ),
              ),
              Spacer(),
              Row(
                children: [
                  _buildAction('正序', 1),
                  4.sizeBoxW,
                  Text(
                    '/',
                    style: textStyle(12, Colors.white.withOpacity(.45), FontWeight.w400),
                  ),
                  4.sizeBoxW,
                  _buildAction('倒序', 0),
                ],
              )
            ],
          ),
          18.sizeBoxH,
          Expanded(
            child: ListView.separated(
              separatorBuilder: (context, index) => Divider(
                height: 0.5,
                color: Colors.white.withOpacity(.05),
              ),
              itemCount: widget.allAudiobooks.length,
              itemBuilder: (BuildContext context, int index) {
                int realIndex = getRealIndex(index);
                final model = widget.allAudiobooks[realIndex];
                bool select = widget.currentPlayIndex == realIndex;
                return InkWell(
                  onTap: () {
                    Get.back(result: realIndex);
                    widget.indexHandler?.call(realIndex);
                  },
                  child: Container(
                      padding: EdgeInsets.only(bottom: 6),
                      child: Column(
                        children: [
                          16.sizeBoxH,
                          Row(
                            children: [
                              if (select) ...[
                                AudioWaveView(),
                                8.sizeBoxW,
                              ],
                              Expanded(
                                child: Text(
                                  '第${model.episodeNumber ?? 0}章 ${model.name ?? ''}',
                                  style: TextStyle(
                                    color: select ? AppColors.actionRed : Colors.white.withOpacity(.9),
                                    fontSize: 14,
                                  ),
                                  maxLines: 1,
                                  overflow: TextOverflow.ellipsis,
                                ),
                              ),
                              8.sizeBoxW,
                              Icon(
                                Icons.navigate_next,
                                size: 18,
                                color: Color(0xffDCDCDC),
                              )
                            ],
                          ),
                          16.sizeBoxH,
                        ],
                      )),
                );
              },
            ),
          )
        ],
      ),
    );
  }

  _buildAction(String title, int index) {
    return GestureDetector(
      onTap: () => setState(() {
        sortType = index;
      }),
      child: Text(
        title,
        style: textStyle(
          12,
          sortType == index ? AppColors.actionRed : Colors.white.withOpacity(.45),
          FontWeight.w400,
        ),
      ),
    );
  }

  int getRealIndex(int index) {
    return (sortType == 1) ? index : widget.allAudiobooks.length - index - 1;
  }
}
